package com.example.newinfinity.Account.MyWorks

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.transition.Visibility
import com.bumptech.glide.Glide
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.grid
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.example.newinfinity.BaseFragment
import com.example.newinfinity.File.FileManager
import com.example.newinfinity.MyViewModel.PhotoBrowserViewModel
import com.example.newinfinity.R
import com.example.newinfinity.databinding.FragmentUserCenterBinding
import com.example.newinfinity.databinding.MyworkAlbumItemLayoutBinding

/*用户中心弹窗中我的作品对应的fragment界面*/
/*显示当前用户保存过的所有图片*/

class UserCenterFragment : BaseFragment<FragmentUserCenterBinding>() {
    private val viewModel:PhotoBrowserViewModel by activityViewModels()
    //保存当前的编辑状态
    private var editState: EditState = EditState.NORMAL

    //保存编辑状态中选中的图片
    private val selectImages: ArrayList<WorksModel> = arrayListOf()

    override fun initViewBinding(): FragmentUserCenterBinding {
        return FragmentUserCenterBinding.inflate(layoutInflater)
    }

    //在该方法中可编写onCreateView时期需要编写的内容
    override fun initView() {
        super.initView()

        //点击返回图标 返回主界面
        mBinding.backImageView.setOnClickListener {
            findNavController().navigate(R.id.action_userCenterFragment_to_homeFragment)
        }

        //监听viewModel中数据的变化
        viewModel.imageModels.observe(this){ model ->
            mBinding.recyclerView.models = model
            //刷新
            mBinding.recyclerView.bindingAdapter.notifyDataSetChanged()
        }

        //配置界面中的recyclerView
        mBinding.recyclerView.grid(5).setup {
            addType<WorksModel>(R.layout.mywork_album_item_layout)
            onBind {
                val binding = getBinding<MyworkAlbumItemLayoutBinding>()
                val model = getModel<WorksModel>()
                //使用glide加载图片
                Glide.with(requireContext()).load(model.thumbnailPath).into(binding.ivThumbnail)

                //是否处于编辑状态
                if (editState == EditState.NORMAL) {
                    binding.ivUnChoose.visibility = View.INVISIBLE
                } else {
                    binding.ivUnChoose.visibility = View.VISIBLE
                }

                //点击缩略图item判断是否选中
                binding.root.setOnClickListener {
                    //先判断是否处于编辑状态
                    if (editState == EditState.NORMAL) {
                        //进入该图片的界面
                        //viewModel中保存当前点击进入详情的图片
                        viewModel.currentClickImageModel = model
                        findNavController().navigate(R.id.action_userCenterFragment_to_photoBrowserFragment)
                    } else {
                        //当前处于编辑状态
                        if (model.clickState == ClickState.NORMAL) {
                            //选中该图片
                            binding.ivSelected.visibility = View.VISIBLE
                            //将该图片加入选中数组中
                            selectImages.add(model)
                            //修改选中状态
                            model.clickState = ClickState.CLICK
                        } else {
                            //取消选中
                            binding.ivSelected.visibility = View.INVISIBLE
                            //将该图片从选中数组中去除
                            selectImages.remove(model)
                            //修改选中状态
                            model.clickState = ClickState.NORMAL
                        }
                    }
                }
            }
        }

        //点击Edit
        mBinding.tvEdit.setOnClickListener {
            if (editState == EditState.NORMAL) {
                //进入编辑状态
                mBinding.tvEdit.text = "Done"
                mBinding.deleteImageView.visibility = View.VISIBLE
                //编辑状态切换
                editState = EditState.EDITING
            } else {
                //退出编辑状态
                mBinding.tvEdit.text = "Edit"
                mBinding.deleteImageView.visibility = View.INVISIBLE
                //切换编辑状态
                editState = EditState.NORMAL
            }
            //刷新recyclerView
            mBinding.recyclerView.bindingAdapter.notifyDataSetChanged()
        }

        //点击删除按钮 删除选中的图片 删除对应路径中的图片
        mBinding.deleteImageView.setOnClickListener {
            //保存外部的数据模型数组中删除对应图片模型
            viewModel.removeAllImages(selectImages)
            //删除对应路径中的图片
            selectImages.forEach { model ->
                FileManager.instance.deleteImage(model.thumbnailPath)
                FileManager.instance.deleteImage(model.originalPath)
            }
            //清空选中图片模型数组
            selectImages.clear()

            //刷新recyclerView
            mBinding.recyclerView.bindingAdapter.notifyDataSetChanged()
        }
    }
}